#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Experimental/Volatility/BlackAtmVolCurve.h>
#pragma unmanaged 
#include <ql\experimental\volatility\abcdatmvolcurve.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Volatility {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IAbcdAtmVolCurve
	public ref class CAbcdAtmVolCurve  : 
            public CBlackAtmVolCurve,
            public Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve
	{
	protected: 
		boost::shared_ptr<QuantLib::AbcdAtmVolCurve>* _ppAbcdAtmVolCurve;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::AbcdAtmVolCurve>* _phAbcdAtmVolCurve;
#endif
		Object^ _AbcdAtmVolCurveOwner;     // reference to object that manages the storage for this object
	internal:
		CAbcdAtmVolCurve (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::IQuote^>^ volsHandles, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Boolean>^>^ inclusionInInterpolationFlag, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ bdc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc);
        CAbcdAtmVolCurve (boost::shared_ptr<QuantLib::AbcdAtmVolCurve>& childNative, Object^ owner);
        CAbcdAtmVolCurve (QuantLib::AbcdAtmVolCurve& childNative, Object^ owner);
        CAbcdAtmVolCurve (CAbcdAtmVolCurve^ copy);
        CAbcdAtmVolCurve (PLATFORM::Type^ t);
#ifdef STRUCT
        CAbcdAtmVolCurve (QuantLib::AbcdAtmVolCurve childNative);
#endif       
#ifdef HANDLE
		CAbcdAtmVolCurve (QuantLib::Handle<QuantLib::AbcdAtmVolCurve>& childNative, Object^ owner);
		CAbcdAtmVolCurve (QuantLib::Handle<QuantLib::AbcdAtmVolCurve> childNative);
#endif
		virtual ~CAbcdAtmVolCurve ();
		!CAbcdAtmVolCurve ();

	internal:
		QuantLib::AbcdAtmVolCurve& GetReference ();
		boost::shared_ptr<QuantLib::AbcdAtmVolCurve>& GetShared ();
		QuantLib::AbcdAtmVolCurve* GetPointer ();
        void SetAbcdAtmVolCurve (boost::shared_ptr<QuantLib::AbcdAtmVolCurve> native)
        {
            if (_ppAbcdAtmVolCurve != NULL)
                delete _ppAbcdAtmVolCurve;
            _ppAbcdAtmVolCurve = new boost::shared_ptr<QuantLib::AbcdAtmVolCurve> (native);
            SetBlackAtmVolCurve (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppAbcdAtmVolCurve));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::AbcdAtmVolCurve>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Double A 
        {
		    virtual Double get () ;
        }
        property Double B 
        {
		    virtual Double get () ;
        }
        property Double C 
        {
		    virtual Double get () ;
        }
        property Double D 
        {
		    virtual Double get () ;
        }
		virtual Double K (Double t) ;
		virtual Cephei::Core::IVector<Double>^ K () ;
        property DateTime MaxDate 
        {
		    virtual DateTime get () ;
        }
        property Double MaxError 
        {
		    virtual Double get () ;
        }
        property Double MaxStrike 
        {
		    virtual Double get () ;
        }
        property Double MinStrike 
        {
		    virtual Double get () ;
        }
        property Cephei::Core::IVector<DateTime>^ OptionDates 
        {
		    virtual Cephei::Core::IVector<DateTime>^ get () ;
        }
        property Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ OptionTenors 
        {
		    virtual Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ get () ;
        }
        property Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ OptionTenorsInInterpolation 
        {
		    virtual Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ get () ;
        }
        property Cephei::Core::IVector<Double>^ OptionTimes 
        {
		    virtual Cephei::Core::IVector<Double>^ get () ;
        }
        property Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^ PerformCalculations 
        {
		    virtual Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^ get () ;
        }
        property Double RmsError 
        {
		    virtual Double get () ;
        }
        property Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^ Update 
        {
		    virtual Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^ get () ;
        }
        property QL::Math::Optimization::EndCriteria::TypeEnum EndCriteria 
        {
		    virtual QL::Math::Optimization::EndCriteria::TypeEnum get () ;
        }
        property Double M446_MaxStrike 
        {
            virtual Double get () = Cephei::QL::Termstructures::IVolatilityTermStructure::MaxStrike::get
            {
                return ((Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^)this)->MaxStrike;
            }
        }
        property Double M446_MinStrike 
        {
            virtual Double get () = Cephei::QL::Termstructures::IVolatilityTermStructure::MinStrike::get
            {
                return ((Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^)this)->MinStrike;
            }
        }
        property DateTime M446_MaxDate 
        {
            virtual DateTime get () = Cephei::QL::ITermStructure::MaxDate::get
            {
                return ((Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^)this)->MaxDate;
            }
        }
        property Cephei::QL::ITermStructure^ M446_Update 
        {
            virtual Cephei::QL::ITermStructure^ get () = Cephei::QL::ITermStructure::Update::get
            {
                return ((Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^)this)->Update;
            }
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IAbcdAtmVolCurve Vector interface
	ref class CAbcdAtmVolCurveVector sealed : 
							    public Core::INativeVector<IAbcdAtmVolCurve^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::AbcdAtmVolCurve>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::AbcdAtmVolCurve>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::AbcdAtmVolCurve>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CAbcdAtmVolCurveVector ();
#ifdef STRUCT
		CAbcdAtmVolCurveVector (std::vector<QuantLib::AbcdAtmVolCurve>& vec, Object^ owner);
		CAbcdAtmVolCurveVector (std::vector<QuantLib::AbcdAtmVolCurve> vec);
#endif       
		CAbcdAtmVolCurveVector (std::vector<boost::shared_ptr<QuantLib::AbcdAtmVolCurve>>& vec, Object^ owner);
#ifdef HANDLE
		CAbcdAtmVolCurveVector (std::vector<QuantLib::Handle<QuantLib::AbcdAtmVolCurve>>& vec, Object^ owner);
#endif
		virtual ~CAbcdAtmVolCurveVector ();
		!CAbcdAtmVolCurveVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IAbcdAtmVolCurve^ default[int] 
		{
			virtual IAbcdAtmVolCurve^ get (int index) = Core::INativeVector<IAbcdAtmVolCurve^>::default::get;
			virtual void set (int index, IAbcdAtmVolCurve^ value) = Core::INativeVector<IAbcdAtmVolCurve^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IAbcdAtmVolCurve^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IAbcdAtmVolCurve^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::AbcdAtmVolCurve>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::AbcdAtmVolCurve>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::AbcdAtmVolCurve>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>::typeid)]
	[FactoryFor(Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve::typeid)]
	[FactoryFor(Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve_Factory::typeid)]
	public ref class CAbcdAtmVolCurve_Factory sealed : public IAbcdAtmVolCurve_Factory
	{
	public:
        virtual IAbcdAtmVolCurve^ Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::IQuote^>^ volsHandles, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Boolean>^>^ inclusionInInterpolationFlag, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ bdc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc);
        /* <summary> Create a hybrid Vector of IAbcdAtmVolCurve, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IAbcdAtmVolCurve^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IAbcdAtmVolCurve, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IAbcdAtmVolCurve^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IAbcdAtmVolCurve^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IAbcdAtmVolCurve^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ CreateNativeVector ()
		{
			return REFNEW CAbcdAtmVolCurveVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Volatility */}
